package com.oig.sys.file.service.impl;

import com.oig.common.constants.CommonEnum;
import com.oig.common.exception.BadRequestException;
import com.oig.sys.file.dao.ITbTemplateFileDao;
import com.oig.sys.file.dto.TemplateFileDto;
import com.oig.sys.file.entity.TbTemplateFile;
import com.oig.sys.file.service.ITbTemplateFileService;
import com.oig.sys.file.vo.TemplateFileVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author wuxl
 */
@Service
public class TbTemplateFileServiceImpl implements ITbTemplateFileService {

	private final ITbTemplateFileDao tbTemplateFileDao;

	@Autowired
	public TbTemplateFileServiceImpl(ITbTemplateFileDao tbTemplateFileDao){
		 this.tbTemplateFileDao = tbTemplateFileDao;
	}


	private TemplateFileVo buildVo(TbTemplateFile templateFile){
		TemplateFileVo templateFileVo = new TemplateFileVo();
		BeanUtils.copyProperties(templateFile, templateFileVo);
		return templateFileVo;
	}

	@Transactional
	@Override
	public TemplateFileVo addTemplateFile(TemplateFileDto templateFileDto){
		TbTemplateFile templateFile = new TbTemplateFile();
		BeanUtils.copyProperties(templateFileDto, templateFile,"id");
		if (CommonEnum.FlagEnum.TRUE_FLAG.getCode().equals(templateFileDto.getReqType())){
			templateFile.setStatus(CommonEnum.CommonStatusEnum.CHECKING.getCode());
		} else {
			templateFile.setStatus(CommonEnum.CommonStatusEnum.INIT.getCode());
		}
		this.tbTemplateFileDao.save(templateFile);
		return buildVo(templateFile);
	}

    @Transactional
	@Override
	public TemplateFileVo updateTemplateFile(TemplateFileDto templateFileDto){
		TbTemplateFile templateFile = this.tbTemplateFileDao.getTemplateFileById(templateFileDto.getId());
		if (CommonEnum.CommonStatusEnum.OK.getCode().equals(templateFile.getStatus())
				||CommonEnum.CommonStatusEnum.CHECKING.getCode().equals(templateFile.getStatus())){
			throw new BadRequestException("生效后不能修改，请重新配置") ;
		}
		BeanUtils.copyProperties(templateFileDto, templateFile,"id");
		this.tbTemplateFileDao.save(templateFile);
		return buildVo(templateFile);
	}


    @Transactional
    @Override
    public TemplateFileVo auditTemplateFile(TemplateFileDto templateFileDto) {
		TbTemplateFile templateFile = this.tbTemplateFileDao.getTemplateFileById(templateFileDto.getId());
		if (!CommonEnum.CommonStatusEnum.CHECKING.getCode().equals(templateFile.getStatus())){
			throw new BadRequestException("非待复核状态") ;
		}
		if (templateFileDto.getTenantId()!=0 && !templateFile.getTenantId().equals(templateFileDto.getTenantId())){
			throw new BadRequestException("无权限") ;
		}
		templateFile.setCheckRemark(templateFileDto.getCheckRemark());
		templateFile.setCheckName(templateFileDto.getCheckName());
		templateFile.setCheckId(templateFileDto.getCheckId());
		templateFile.setCheckTime(templateFileDto.getCheckTime());
		if (CommonEnum.FlagEnum.TRUE_FLAG.getCode().equals(templateFileDto.getReqType())){
			templateFile.setStatus(CommonEnum.CommonStatusEnum.OK.getCode());
		} else {
			templateFile.setStatus(CommonEnum.CommonStatusEnum.UN_ENABLE.getCode());
		}
		this.tbTemplateFileDao.save(templateFile);
		return buildVo(templateFile);
    }

    @Transactional
    @Override
	public void cancelTemplateFile(Integer id){
		TbTemplateFile templateFile = this.tbTemplateFileDao.getTemplateFileById(id);
		templateFile.setStatus(CommonEnum.CommonStatusEnum.DEL.getCode());
		this.tbTemplateFileDao.save(templateFile);
	}


	@Override
	public TemplateFileVo getTemplateFile(Integer id) {
		TbTemplateFile templateFile = this.tbTemplateFileDao.getTemplateFileById(id);
		return buildVo(templateFile);
	}
}
